﻿
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ServiceModel.Channels;
using Windows.UI.Popups;
using Windows.UI.Xaml.Navigation;
using CityDashboard.Models;
using CityDashboard.Services;
using CityDashboard.Views;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Mvvm.Interfaces;

namespace CityDashboard.ViewModels
{
    public class MainViewModel : ViewModel
    {
        private readonly INavigationService _navigationService;

        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }


        public static ThongKe ThongKeData;
        public ThongKe KetQuaThongKe
        {
            get { return ThongKeData; }
            set { SetProperty(ref ThongKeData, value); }
        }

        public DelegateCommand<object> XemChiTietCommand { get; set; }

        public DelegateCommand<object> XemQuocGiaCommand { get; set; }

        public DelegateCommand<object> XemNhaDauTuCommand { get; set; }

        public DelegateCommand<object> TimHoSoCommand { get; set; }

        public DelegateCommand<object> XemHoSoCommand { get; set; }



        public MainViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
            var data = new DataService();
            XemChiTietCommand = new DelegateCommand<object>(XemChiTiet);
            XemQuocGiaCommand = new DelegateCommand<object>((e) => _navigationService.Navigate("QuocGiaChiTiet", this));
            XemNhaDauTuCommand = new DelegateCommand<object>((e) => _navigationService.Navigate("TopNhaDauTu", this));
            XemHoSoCommand = new DelegateCommand<object>((e) => _navigationService.Navigate("HoSoChiTiet", e));
            TimHoSoCommand = new DelegateCommand<object>(async (e) =>
            {
                var error = false;
                var result = new List<HoSoChiTiet>();
                try
                {
                    result = await data.TimHoSo(e.ToString());

                }
                catch (Exception)
                {

                    error = true;
                }
                if (error || result == null || result.Count == 0)
                {
                    var dialog = new MessageDialog("Không tìm thấy hồ số theo mã: " + e.ToString(),
                        "City Dashboard");
                    await dialog.ShowAsync();
                }
                else
                {
                    _navigationService.Navigate("HoSoDangGiaiQuyetChiTiet", result);

                }
            });
        }

        private void XemChiTiet(object obj)
        {
            _navigationService.Navigate("HoSoDangGiaiQuyetChiTiet", obj);
        }


        public override void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            KetQuaThongKe = (ThongKe)navigationParameter;

            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
        }
    }
}
